package com.tledu.controller;

import com.tledu.model.AjaxResult;
import com.tledu.model.Department;
import com.tledu.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
//实例化
@Controller
//执行Mapper
@RequestMapping("/Department")
//@RestController
public class DepartmentController {
    //自动装配
    @Autowired
    //将DepartmentService注入进来
    private DepartmentService departmentService;
    //部门管理
    @RequestMapping("/department")
    public String department(Model model, String cpage){
        //查询数据库部门列表
        //当前页
        if(cpage==null){
            cpage="1";
        }
        //分页单位
        int pageSize = 3;
        Map<String,Object> map = new HashMap<>();
        map.put("cpage",Integer.parseInt(cpage));
        map.put("pageSize",pageSize);
        List<Department> departmentList = departmentService.department(map);
        model.addAttribute("department_list",departmentList);
        //总条数
        int totalCount = departmentService.departmentCount();
        //总页数
        int totalPage = totalCount/pageSize + (totalCount % pageSize == 0?0:1);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("cpage",cpage);
        return "department";
    }

    //跳转到富文本,显示note
    @RequestMapping("/richtext")
    public String richtext(Model model,Integer department_id){
        String note = departmentService.showNote(department_id);
        model.addAttribute("department_note",note);
        model.addAttribute("department_id",department_id);
        return "department_richtext";
    }
    //点击确定修改之后 , 跳转到部门管理页面
    @RequestMapping("/toDepartment")
    @ResponseBody
    public AjaxResult toDepartment(){
        return new AjaxResult(null,null,true,null);
    }
    //富文本获取note, 并确定修改来更新内容
    @RequestMapping("/updateNote")
    @ResponseBody
    public AjaxResult updateNote(String d_note, Integer department_id){
        Map<String,Object> map = new HashMap<>();
        map.put("d_note",d_note);
        map.put("department_id",department_id);
        departmentService.updateNote(map);
        return new AjaxResult(null,null,true,null);
    }

    //添加部门
    //跳转到添加页面
    @RequestMapping("/Add_01")
    public String add_01(){
        return "department_add";
    }
    @RequestMapping("/Add")
    @ResponseBody
    public AjaxResult add(Department department){
        try {
            int i = departmentService.Add(department);
            if(i > 0){
                return new AjaxResult(null,null,true,null);
            }else{
                return new AjaxResult(null,null,false,null);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(e.getMessage(),null,false,null);
        }
    }

    //删除
    @RequestMapping("/Delete")
    @ResponseBody //处理的是ajax请求,将返回值转换成json格式发送给前台
    // response.getWriter().printl();
    public AjaxResult Delete(int department_id){
        departmentService.Delete_id(department_id);
        return new AjaxResult(null,null,true,null);
    }

    //按照id进行搜索
    @RequestMapping("Select_id")
    public String Select_id(Model model,String department_id){
        List<Department> department_list = departmentService.Sclect_id(department_id);
        model.addAttribute("department_list",department_list);
        return "department";
    }

    //批量删除
    @RequestMapping("/deleteAll")
    public String DeleteAll(String[] ids){
            for (String department_id : ids){
                departmentService.DeleteAll(department_id);
            }
            return null;
    }
}
